package com.moto.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moto.entity.MotoUser;
import com.moto.service.UserService;

@Scope("prototype")
@RequestMapping("/user")
@Controller
public class UserController {

	private static final Logger LOG = Logger.getLogger(UserController.class);

	@Autowired
	private UserService userService;

	/**
	 * 查询所有用户
	 */
	@RequestMapping(value = "/users", method = RequestMethod.GET)
	public String getUser(Model model, @RequestParam("pageNum") int pageNum,
			@RequestParam("pageSize") int pageSize) {
		LOG.debug("分页查询用户信息，参数：pageNum = " + pageNum + ",pageSize = " + pageSize);
		
		PageHelper.startPage(pageNum, pageSize);
		List<MotoUser> users = userService.userList();
		PageInfo<MotoUser> pageInfo = new PageInfo<MotoUser>(users);
		model.addAttribute("page", pageInfo);
		return "user";
	}
	
	/**
	 * 查询所有用户，不分页
	 */
	@RequestMapping(value = "/userList", method = RequestMethod.GET)
	public String getUser(Model model) {
		LOG.debug("查询用户信息");
		List<MotoUser> users = userService.userList();
		model.addAttribute("userList",users);
		return "user";
	}


	/**
	 * 用户锁定方法
	 */
	@RequestMapping(value = "/locking", method = RequestMethod.GET)
	@ResponseBody
	public String locking(@RequestParam("userId") String userId) {
		LOG.debug("调用锁定用户方法，参数：" + userId);
		boolean locking = userService.locking(userId);
		if (locking) {
			return Boolean.TRUE.toString();
		} else {
			return Boolean.FALSE.toString();
		}
	}


	/**
	 * 用户解锁方法
	 */
	@RequestMapping(value = "/deblocking", method = RequestMethod.GET)
	@ResponseBody
	public String deblocking(@RequestParam("userId") String userId) {
		LOG.debug("调用解锁用户方法，参数：" + userId);
		boolean deblocking = userService.deblocking(userId);
		if (deblocking) {
			return Boolean.TRUE.toString();
		} else {
			return Boolean.FALSE.toString();
		}
	}
	
	/**
	 * 用户登陆方法
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(Model model,@RequestParam(value="username",required=false)String username,
	                    @RequestParam(value="password",required=false)String password,
	                    HttpSession httpSession){
		LOG.info("用戶登陸，用戶名："+username+",密碼："+password);
		if(username.trim().equals("xuyan")&&password.trim().equals("xuyan123456")){
			httpSession.setAttribute("username", "许岩");
			return "redirect:/index.jsp";
		}else{
			return "redirect:/login.jsp";
		}
	}
}
